﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace RepairTracker
{
    public partial class workflow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderUI();
            }
        }

        protected void radioWorkflow_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetPanelControls();
            mviewWorkflow.ActiveViewIndex = int.Parse(radioWorkflow.SelectedValue);

        }

        private void RenderUI() 
        {
            List<Device> devices = Entity.GetAllDevices();
            lstDevice.Items.Add(new ListItem("", "0".ToString()));
            lstInsertDevice.Items.Add(new ListItem("", "0".ToString()));
            radioWorkflow.SelectedIndex = 0;
            radioWorkflow_SelectedIndexChanged(null, null);
            foreach (Device  device in devices)
            {
                lstDevice.Items.Add(new ListItem(device.DeviceName, device.Id.ToString()));
                lstInsertDevice.Items.Add(new ListItem(device.DeviceName, device.Id.ToString()));

            }

            List<District> districts = Entity.GetAllDistricts();
            lstDistrict.Items.Add(new ListItem("", "0".ToString()));
            lstInsertDistrict.Items.Add(new ListItem("", "0".ToString()));
            foreach (District district in districts)
            {
                lstDistrict.Items.Add(new ListItem(district.DistrictName, district.Id.ToString()));
                lstInsertDistrict.Items.Add(new ListItem(district.DistrictName, district.Id.ToString()));

            }

            lstOwner.Items.Add(new ListItem("", "0".ToString()));


            //List<Fault> faults = Entity.GetAllFaults();
            //lstInputFault.Items.Add(new ListItem("", "0".ToString()));
            //lstInsertFault.Items.Add(new ListItem("", "0".ToString()));
            //lstRepairFault.Items.Add(new ListItem("", "0".ToString()));
            //foreach (Fault fault in faults)
            //{
            //    lstInputFault.Items.Add(new ListItem(fault.FaultCode + fault.FaultName, fault.Id.ToString()));
            //    lstInsertFault.Items.Add(new ListItem(fault.FaultCode + fault.FaultName, fault.Id.ToString()));
            //    lstRepairFault.Items.Add(new ListItem(fault.FaultCode + fault.FaultName, fault.Id.ToString()));
            //}
            
            //set default viewid count on cookie
        }

        protected void btnSearchBySerialNo_Click(object sender, EventArgs e)
        {
            txtSearchSerialNo.Text=txtSearchSerialNo.Text.Replace("S/N:", "").Trim();
            if ( txtSearchSerialNo.Text != string.Empty)
            {
                dataProduct.DataSourceID = "sourceProductSearch";
                dataProduct.DataBind();
                if (dataProduct.Rows.Count == 0)
                {
                    
                    panelInsertAndInput.Enabled = true;
                    txtInsertSerialNo.Text = txtSearchSerialNo.Text;
                    txtInsertPsamNo.Focus();
                    //lblError.Text = "什么记录都没搜索到";
                }
                else
                {
                    panelInput.Enabled = true;
                    dataProduct.SelectedIndex = 0;
                    dataProduct_SelectedIndexChanged(sender, e);
                }
            }
        }

        protected void lstDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            int districtId=int.Parse( lstDistrict.SelectedValue);
            if (districtId!=0)
            {
                List<Owner> owners = Entity.GetOwnersByDistrictId(districtId);
                lstOwner.Items.Clear();
                lstOwner.Items.Add(new ListItem("", "0".ToString()));
                foreach (Owner owner in owners)
                {
                    lstOwner.Items.Add(new ListItem(owner.DisplayName, owner.Id.ToString()));
                }
                
            }
        }

        protected void lstOwner_SelectedIndexChanged(object sender, EventArgs e)
        {

            dataProduct.DataSourceID = "sourceProduct";
            dataProduct.DataBind();

        }

        protected void lstDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataProduct.DataSourceID = "sourceProduct";
            dataProduct.DataBind();
        }

        protected void dataProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Fault> faults;
            switch (radioWorkflow.SelectedValue)
            {
                case "1":
                    panelRepair.Enabled = true;

                    faults = Entity.GetFaultsByDevice((int)dataProduct.SelectedDataKey["DeviceId"]);
                    lstRepairFault.Items.Clear();
                    lstRepairFault.Items.Add(new ListItem("", "0".ToString()));
                    foreach (Fault fault in faults)
                    {
                        lstRepairFault.Items.Add(new ListItem(fault.FaultCode + fault.FaultName, fault.Id.ToString()));
                    }

                    break;
                case "2":
                    lblOutputSerialNo.Text = dataProduct.SelectedDataKey["SerialNo"].ToString();
                    lblOutputPsamNo.Text = dataProduct.SelectedDataKey["PsamNo"].ToString();
                    lblOutputVersion.Text = dataProduct.SelectedDataKey["Version"].ToString();
                    panelOutput.Enabled = true;

                    break;

                case "3":
                    lblInputSerialNo.Text = dataProduct.SelectedDataKey["SerialNo"].ToString();
                    lblInputPsamNo.Text = dataProduct.SelectedDataKey["PsamNo"].ToString();
                    lblInputVersion.Text = dataProduct.SelectedDataKey["Version"].ToString();
                    panelInput.Enabled = true;

                    faults = Entity.GetFaultsByDevice((int)dataProduct.SelectedDataKey["DeviceId"]);
                    lstInputFault.Items.Clear();
                    lstInputFault.Items.Add(new ListItem("", "0".ToString()));
                    foreach (Fault fault in faults)
                    {
                        lstInputFault.Items.Add(new ListItem(fault.FaultCode + fault.FaultName, fault.Id.ToString()));
                    }
                    break;
                default:
                    break;
            }
        }

        protected void btnInsertAndInput_Click(object sender, EventArgs e)
        {
            int deviceId, ownerId, requestFaultId;
            string serialNo, psamNo, version;
            if (txtInsertSerialNo.Text.Length == 0)
            {
                lblInsertError.Text = "请输入序列号。";
                return;
            }
            //districtId = int.Parse(lstInsertDistrict.SelectedValue);
            //if (districtId == 0)
            //{
            //    lblInsertError.Text = "请选择区县。";
            //    return;
            //}
            ownerId = int.Parse(lstInsertOwner.SelectedValue);
            if (ownerId == 0)
            {
                lblInsertError.Text = "请选择网点。";
                return;
            }
            deviceId = int.Parse(lstInsertDevice.SelectedValue);
            if (deviceId == 0)
            {
                lblInsertError.Text = "请选择设备类型。";
                return;
            }

            requestFaultId = int.Parse(lstInsertFault.SelectedValue);
            if (requestFaultId==0)
            {
                lblInsertError.Text = "请选择故障类型。";
                return;
            }

            serialNo = txtInsertSerialNo.Text.Replace("S/N:","").Trim();
            if (serialNo.Length>8)
            {
                lblInsertError.Text = "序列号长度太长。";
                return;
            }
            else if (serialNo.Length <8 &&lstInsertDevice.SelectedValue!="1")
            {
                lblInsertError.Text = "你的太短了！！！";
                return;
            }

            psamNo= txtInsertPsamNo.Text.Replace("PSAM:","").Trim();
            version=txtInsertVersion.Text.Trim();

            int ret=
                Entity.ProductInsertAndInput(deviceId, ownerId,serialNo ,psamNo,version, requestFaultId);
            if (ret==-1)
            {
                lblInsertError.Text = "入库失败，可能有重复记录。";
            }
            else
            {
                ResetPanelControls();
                lblInsertError.Text = "入库成功。";
            }
        }

        private void ResetPanelControls() 
        {
            txtInsertSerialNo.Text = "";
            txtInsertPsamNo.Text = "";
            txtInsertVersion.Text = "";
            lblInputSerialNo.Text = "";
            lblInputPsamNo.Text = "";
            lblInputVersion.Text = "";

            txtRepairGear.Text = "";
            txtRepairMethod.Text = "";

            panelRepair.Enabled = false;
            panelOutput.Enabled = false;
            panelInput.Enabled = false;
            panelInsertAndInput.Enabled = false;
            lblInputError.Text = "";
            lblInsertError.Text = "";
            lblRepairError.Text = "";
            lblOutputError.Text = "";
            dataProduct.DataBind();
            dataProduct.SelectedIndex = -1;

            lstDistrict.SelectedIndex = 0;
            lstOwner.SelectedIndex = 0;
            lstDevice.SelectedIndex = 0;
            txtSearchSerialNo.Text = "";

            txtSearchSerialNo.Focus();
            switch (radioWorkflow.SelectedValue)
            {
                case"1":
                    
                    break;
                case"2":
                    
                    break;
                case"3":
                    break;
             
            }
            
        }

        protected void lstInsertDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            int districtId = int.Parse(lstInsertDistrict.SelectedValue);
            if (districtId != 0)
            {
                List<Owner> owners = Entity.GetOwnersByDistrictId(districtId);
                lstInsertOwner.Items.Clear();
                lstInsertOwner.Items.Add(new ListItem("", "0".ToString()));
                foreach (Owner owner in owners)
                {
                    lstInsertOwner.Items.Add(new ListItem(owner.DisplayName, owner.Id.ToString()));
                }

            }
        }

        protected void btnInput_Click(object sender, EventArgs e)
        {
            int requestFaultId;
            requestFaultId = int.Parse(lstInputFault.SelectedValue);
            if (requestFaultId == 0)
            {
                lblInputError.Text = "请选择故障类型。";
                return;
            }

            int ret =
                Entity.ProductInput((int)dataProduct.SelectedDataKey["Id"], requestFaultId);

            if (ret == -1)
            {
                lblInputError.Text = "入库失败。";
            }
            else
            {
                
                ResetPanelControls();
                lblInputError.Text = "入库成功。";
            }
        }

        protected void btnRepair_Click(object sender, EventArgs e)
        {
            int realFaultId;
            string repairMethod, repairGear;

            realFaultId = int.Parse(lstRepairFault.SelectedValue);
            if (realFaultId == 0)
            {
                lblRepairError.Text = "请选择故障代码";
                return;
            }

            repairMethod = txtRepairMethod.Text;
            if (repairMethod.Length == 0)
            {
                lblRepairError.Text = "请填写维修内容";
                return;
            }

            repairGear = txtRepairGear.Text;
            if (repairGear.Length == 0)
            {
                lblRepairError.Text = "备件名称。";
                return;
            }

            int ret =
                Entity.ProductRepaired((int)dataProduct.SelectedDataKey["Id"], realFaultId, "", repairMethod, repairGear);

            if (ret == -1)
            {

                lblRepairError.Text = "提交失败。";
            }
            else
            {
                ResetPanelControls();
                lblRepairError.Text = "提交成功。";
            }
        }


        protected void btnOutput_Click(object sender, EventArgs e)
        {
            int ret=
                Entity.ProductOutput((int)dataProduct.SelectedDataKey["Id"]);
            if (ret==-1)
            {
                lblOutputError.Text = "出库失败";
            }
            else
            {
                ResetPanelControls();
                lblOutputError.Text = "出库成功。";
            }
        }

        protected void lstRepairFault_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstRepairFault.SelectedIndex == 1)
            {
                txtRepairMethod.Text = "无";
                txtRepairGear.Text = "无";
            }
            else
            {
                txtRepairMethod.Text = "";
                txtRepairGear.Text = "";
            }
        }

        protected void lstInputFault_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lstInsertDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Fault> faults = Entity.GetFaultsByDevice(int.Parse(lstInsertDevice.SelectedValue));
            lstInsertFault.Items.Clear();
            lstInsertFault.Items.Add(new ListItem("", "0".ToString()));
            foreach (Fault fault in faults)
            {
                lstInsertFault.Items.Add(new ListItem(fault.FaultCode + fault.FaultName, fault.Id.ToString()));
            }
            
        }
    }
}